//
//  NSBundle+extension.swift
//  MyLottery
//
//  Created by wujunjie on 2018/8/12.
//  Copyright © 2018年 wujunjie. All rights reserved.
//

import Foundation
let hmWeiboCurrentVersion = "hmWeiboCurrentVersion"

extension Bundle {
    //1 获取当前版本
    class func getCurrenVersion () -> String {
        let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        
        return version
    }
    
    //2 比较是否是新版本
    class func isNewVersion() -> Bool {
        //获取当前版本
        let currentVersion = getCurrenVersion() //1.2
        
        //获取老版本
        if let oldVersion = UserDefaults.standard.object(forKey: hmWeiboCurrentVersion) as? String {
            saveCurrentVersion()
            return currentVersion != oldVersion
        }
        
        saveCurrentVersion()
        
        return true
    }
    
    //3 存储当前版版
    class func saveCurrentVersion () {
        //1. 取到当前版本
        let currentVersion = getCurrenVersion()
        //2. 存起来
        UserDefaults.standard.set(currentVersion, forKey: hmWeiboCurrentVersion)
    }
}
